package com.example.demo.test;

import com.example.demo.entity.UserEntity;

import java.util.Arrays;
import java.util.List;

/**
 * @author: YanMing.Wang
 * @date: 2024-03-13
 * @desp: 交行面试题
 */
public class JiaoHangExam {

    public static void main(String[] args) {
        String s = "!\\|";
        s = s.replaceAll("!", "\\\\!");
       // System.out.println(s);
        s = s.replaceAll("\\|", "\\\\|");
       // System.out.println(s);
        s = s.replaceAll("\\\\", "\\\\\\\\");
        System.out.println(s);
        String str = "hello";
        System.out.println(str.contains("") + "," + str.contains(" ") + "," + str.contains("".toUpperCase()) + "," + "".contains(str));
        forEach();
        // judge();
        for (int i = 1; i < 7; i++) {
            int value = fibonacci(i);
            System.out.println(value);
        }
        int[] array = {48, 20, 32, 53, 29, 60, 30, 200, 89, 34, 98};
        Arrays.sort(array);
        System.out.println(Arrays.toString(array));
        int index = binarySearch(array, 60);
        System.out.println(index);
    }

    /**
     * 判断题
     *
     * @return
     */
    public static int judge() {
        // 1.输出结果--12Hello World!:正确
        System.out.println(4 + 8 + "Hello World!");
        // 2.一个java源文件可以有多个类,但只能有一个类是public的:正确

        //
        int i = 10;
        do {
            i /= 2;
            System.out.println(i);
        } while (i-- > 1);
        System.out.println(i);

        Integer a = new Integer(9);
        Integer b = new Integer(9);
        System.out.println(a == b);
        UserEntity user = new UserEntity();
        user.setId(123001);
        user.setName("王宇凡");
        System.out.println(user.toString());
        System.out.println("    ".contains(" "));
        return 0;
    }

    /**
     * 跳出foreach
     */
    public static void forEach() {
        String[] s = {"a", "b", "c"};
        List<String> list = Arrays.asList(s);
        for (String str : list) {
            if (str.equals("b")) {
                System.out.println("if:" + str);
                break;
            }
            System.out.println("else:" + str);
        }
    }

    /**
     * 费伯纳西数列
     *
     * @param n
     * @return
     */
    public static int fibonacci(int n) {
        if (n <= 1) {
            return n;
        }
        int[] dp = new int[n + 1];
        dp[0] = 0;
        dp[1] = 1;
        for (int i = 2; i <= n; i++) {
            dp[i] = dp[i - 1] + dp[i - 2];
        }
        return dp[n];
    }


    /**
     * java二分法查找数组元素
     *
     * @param array
     * @param target
     * @return
     */
    public static int binarySearch(int[] array, int target) {
        int low = 0;
        int high = array.length - 1;

        while (low <= high) {
            int mid = low + (high - low) / 2;  // 计算中间位置

            if (array[mid] == target) {
                return mid;  // 找到目标元素，返回索引
            } else if (array[mid] < target) {
                low = mid + 1;  // 目标元素在右半部分，更新左边界
            } else {
                high = mid - 1;  // 目标元素在左半部分，更新右边界
            }
        }

        return -1;  // 没有找到目标元素

    }
}
